package com.rc.saas.tenant.service.member;

import com.rc.saas.tenant.mapper.channel.RcChannelApplyMapper;
import com.rc.saas.tenant.mapper.member.RcMemberFlagMapper;
import com.rc.saas.tenant.mapper.operator.RcOperatorMapper;
import com.rc.saas.tenant.model.channel.RcChannelApply;
import com.rc.saas.tenant.model.channel.RcChannelGainRecord;
import com.rc.saas.tenant.model.channel.RcChannelGainSetting;
import com.rc.saas.tenant.model.channel.RcChannelGainSettingExample;
import com.rc.saas.tenant.model.enterprise.EnterpriseMember;
import com.rc.saas.tenant.model.enterprise.EnterpriseSubmemberApply;
import com.rc.saas.tenant.model.member.MemberInfo;
import com.rc.saas.tenant.model.member.RcMemberFlag;
import com.rc.saas.tenant.model.member.RcMemberFlagExample;
import com.rc.saas.tenant.model.operator.RcOperator;
import com.rc.saas.tenant.service.channel.RcChannelGainRecordService;
import com.rc.saas.tenant.service.channel.RcChannelGainSettingService;
import com.rc.saas.tenant.service.enterprise.EnterpriseMemberService;
import com.rc.saas.tenant.service.enterprise.EnterpriseSubmemberApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RcMemberFlagServiceImpl implements RcMemberFlagService{

    @Autowired
    private RcMemberFlagMapper memberFlagMapper;

    @Autowired
    private EnterpriseSubmemberApplyService enterpriseSubmemberApplyService;

    @Autowired
    private MemberInfoService memberInfoService;

    @Autowired
    private EnterpriseMemberService enterpriseMemberService;

    @Autowired
    private RcOperatorMapper rcOperatorMapper;

    @Autowired
    private RcChannelApplyMapper channelApplyMapper;

    @Autowired
    private RcChannelGainRecordService channelGainRecordService;

    @Autowired
    private RcChannelGainSettingService channelGainSettingService;




    @Override
    public RcMemberFlagMapper getMapper() {
        return memberFlagMapper;
    }

    @Override
    public void addChannel(Long memberId, Long enterpriseId) {
        RcMemberFlag memberFlag = memberFlagMapper.selectByPrimaryKey(memberId);
        if (memberFlag != null) {
            memberFlag.setIsEnterpriseLevel("1");
            memberFlag.setEnterpriseId(enterpriseId);
            memberFlag.setChannelLevel("S0");
            memberFlag.setIsChannelLevel("1");
            memberFlag.setLastUpdatedTime(new Date());
            memberFlagMapper.updateByPrimaryKeySelective(memberFlag);
        } else {
            memberFlag = new RcMemberFlag();
            memberFlag.setIsEnterpriseLevel("1");
            memberFlag.setMemberId(memberId);
            memberFlag.setChannelLevel("S0");
            memberFlag.setIsChannelLevel("1");
            memberFlag.setEnterpriseId(enterpriseId);
            memberFlagMapper.insertSelective(memberFlag);
        }

        EnterpriseSubmemberApply submemberApply = enterpriseSubmemberApplyService.selectByMember(memberId);
        MemberInfo memberInfo = memberInfoService.selectByPrimaryKey(memberId);
        EnterpriseMember enterprise = enterpriseMemberService.selectByPrimaryKey(enterpriseId);

        if (submemberApply == null) {
            submemberApply = new EnterpriseSubmemberApply();
            submemberApply.setTenantCode("MA4T4JPE8");
            submemberApply.setEnterpriseId(enterpriseId);
            submemberApply.setApplySource("1");
            submemberApply.setApplyStatus("1");
            if (memberInfo != null) {
                submemberApply.setNickname(memberInfo.getNickname());
                submemberApply.setBirthday(memberInfo.getBirthday());
                submemberApply.setAddress(memberInfo.getAddress());
                submemberApply.setRecommenderId(memberInfo.getRecommenderId());
                submemberApply.setPhone(memberInfo.getMobile());
                submemberApply.setNameWx(memberInfo.getNickname());

            }
            if (enterprise != null) {
                submemberApply.setEnterpriseName(enterprise.getEnterpriseName());
                submemberApply.setEnterpriseOwner(enterprise.getEnterpriseOwner());
                submemberApply.setOwnerMobile(enterprise.getOwnerMobile());
            }

            submemberApply.setMemberId(memberId);
            enterpriseSubmemberApplyService.insertSelective(submemberApply);
        } else {
            submemberApply.setApplyStatus("1");
            enterpriseSubmemberApplyService.updateByPrimaryKeySelective(submemberApply);
        }


    }

    @Override
    public void delChannel(Long memberId) {
        RcMemberFlag memberFlag = memberFlagMapper.selectByPrimaryKey(memberId);
        memberFlag.setIsChannelLevel("0");
        memberFlag.setChannelLevel("");
        memberFlag.setLastUpdatedTime(new Date());
        memberFlagMapper.updateByPrimaryKeySelective(memberFlag);
    }

    public void addOperator(Long operatorId, Long memberId, Long enterpriseId) {
        RcOperator operator = rcOperatorMapper.selectByPrimaryKey(operatorId);

        RcMemberFlag memberFlag = memberFlagMapper.selectByPrimaryKey(memberId);
        if (memberId == null || memberId == 0) {
            return;
        }

        if (memberFlag != null) {
            memberFlag.setIsOperator("1");
            memberFlag.setLastUpdatedTime(new Date());
        } else {
            memberFlag = new RcMemberFlag();
            memberFlag.setIsOperator("1");
            memberFlag.setEnterpriseId(enterpriseId);
        }

        EnterpriseSubmemberApply submemberApply = enterpriseSubmemberApplyService.selectByMember(memberId);
        MemberInfo memberInfo = memberInfoService.selectByPrimaryKey(memberId);
        EnterpriseMember enterprise = enterpriseMemberService.selectByPrimaryKey(enterpriseId);

        if (submemberApply == null) {
            submemberApply = new EnterpriseSubmemberApply();
            submemberApply.setTenantCode("MA4T4JPE8");
            submemberApply.setEnterpriseId(enterpriseId);
            submemberApply.setApplySource("1");
            submemberApply.setApplyStatus("1");
            if (memberInfo != null) {
                submemberApply.setNickname(memberInfo.getNickname());
                submemberApply.setBirthday(memberInfo.getBirthday());
                submemberApply.setAddress(memberInfo.getAddress());
                submemberApply.setRecommenderId(memberInfo.getRecommenderId());
                submemberApply.setPhone(memberInfo.getMobile());
                submemberApply.setNameWx(memberInfo.getNickname());

            }
            if (enterprise != null) {
                submemberApply.setEnterpriseName(enterprise.getEnterpriseName());
                submemberApply.setEnterpriseOwner(enterprise.getEnterpriseOwner());
                submemberApply.setOwnerMobile(enterprise.getOwnerMobile());
            }

            submemberApply.setMemberId(memberId);
            enterpriseSubmemberApplyService.insertSelective(submemberApply);
        } else {
            submemberApply.setApplyStatus("1");
            enterpriseSubmemberApplyService.updateByPrimaryKeySelective(submemberApply);
        }
        memberFlag.setIsEnterpriseLevel("1");
        memberFlag.setEnterpriseId(enterpriseId);


        RcChannelApply channelApply = channelApplyMapper.selectByMemberId(memberId);
        if (channelApply == null || channelApply.getChannelApplyId() == null) {
            channelApply = new RcChannelApply();
            channelApply.setChannelName(operator.getContactName());
            channelApply.setEnterpriseId(enterpriseId);
            channelApply.setMemberId(memberId);
            channelApply.setPrincipal(operator.getContactName());
            channelApply.setMobile(operator.getPhone());
            channelApply.setStatus("1");
            channelApply.setApplyStatus("1");
            channelApply.setAddress(operator.getAddress());
            channelApply.setChannelType("1");
            channelApply.setChannelLevel("S0");
            channelApplyMapper.insertReturnId(channelApply);

            RcChannelGainSettingExample gainSettingExample = new RcChannelGainSettingExample();
            gainSettingExample.createCriteria().andChannelLevelEqualTo("S0");
            List<RcChannelGainSetting> gainSettingList = channelGainSettingService.selectByExample(gainSettingExample);
            for (RcChannelGainSetting gainSetting:gainSettingList) {
                RcChannelGainRecord gainRecord = new RcChannelGainRecord();
                gainRecord.setChannelLevel(gainSetting.getChannelLevel());
                gainRecord.setMemberId(channelApply.getMemberId());
                gainRecord.setChannelId(channelApply.getChannelApplyId());
                gainRecord.setGainName(gainSetting.getGainName());
                gainRecord.setGainType(gainSetting.getGainType());
                gainRecord.setGainLogo(gainSetting.getGainLogo());
                gainRecord.setRebateRatio(gainSetting.getRebateRatio());
                gainRecord.setGainExplain(gainSetting.getGainExplain());
                gainRecord.setSort(gainSetting.getSort());
                channelGainRecordService.insertSelective(gainRecord);
            }

        }

        if (memberFlag.getIsChannelLevel() == null || "0".equals(memberFlag.getIsChannelLevel())) {
            memberFlag.setIsChannelLevel("1");
            memberFlag.setChannelLevel("S0");
        }

        if (memberFlag.getMemberId() != null) {
            memberFlagMapper.updateByPrimaryKeySelective(memberFlag);
        } else {
            memberFlag.setMemberId(memberId);
            memberFlagMapper.insertSelective(memberFlag);
        }
    }

    @Override
    public void delOperator(Long memberId) {
        RcMemberFlag memberFlag = memberFlagMapper.selectByPrimaryKey(memberId);
        memberFlag.setIsOperator("0");
        memberFlag.setLastUpdatedTime(new Date());
        memberFlagMapper.updateByPrimaryKeySelective(memberFlag);

    }

    @Override
    public List<RcMemberFlag> listMemberFlag(List<Long> idList) {
        return memberFlagMapper.listMemberFlag(idList);
    }
}
